﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace Escape
{
    class ItemBar
    {

        static Texture2D tex;
        static Rectangle pos;

        static Texture2D selection;

        static int scale = 2;
        static int selectedItemIndex = 1;
        static int scrollOld = 0;

        static Item[] items;

        public static Item currentItem;

        public static void Init()
        {
            tex = Basic.content.Load<Texture2D>("HUD\\items");
            selection = Basic.content.Load<Texture2D>("HUD\\selection");

            pos = new Rectangle(Basic.dManager.PreferredBackBufferWidth / 2 - tex.Width * scale/ 2,
                Basic.dManager.PreferredBackBufferHeight - tex.Height * scale,
                tex.Width * scale, tex.Height * scale);

            items = new Item[10];

            for (int i = 0; i < 10; i++)
                items[i] = new ZeroItem();
        }

        public static void setItem(Item item, int index)
        {
            items[index] = item;
        }

        public static bool setItemToNextZeroSlot(Item item)
        {
            for (int i = 0; i < 10; i++)
            {
                if (items[i] is ZeroItem)
                {
                    items[i] = item;
                    return true;
                }
            }

            return false;
        }

        private static void Update()
        {

            currentItem = items[selectedItemIndex];

            KeyboardState state = Keyboard.GetState();

            if (scrollOld < Mouse.GetState().ScrollWheelValue)
                selectedItemIndex -= 1;
            if (scrollOld > Mouse.GetState().ScrollWheelValue)
                selectedItemIndex += 1;

            if (state.IsKeyDown(Keys.D1))
                selectedItemIndex = 0;
            if (state.IsKeyDown(Keys.D2))
                selectedItemIndex = 1;
            if (state.IsKeyDown(Keys.D3))
                selectedItemIndex = 2;
            if (state.IsKeyDown(Keys.D4))
                selectedItemIndex = 3;
            if (state.IsKeyDown(Keys.D5))
                selectedItemIndex = 4;
            if (state.IsKeyDown(Keys.D6))
                selectedItemIndex = 5;
            if (state.IsKeyDown(Keys.D7))
                selectedItemIndex = 6;
            if (state.IsKeyDown(Keys.D8))
                selectedItemIndex = 7;
            if (state.IsKeyDown(Keys.D9))
                selectedItemIndex = 8;
            if (state.IsKeyDown(Keys.D0))
                selectedItemIndex = 9;

            scrollOld = Mouse.GetState().ScrollWheelValue;
            selectedItemIndex = (int)MathHelper.Clamp(selectedItemIndex, 0, 9);
            
        }

        static Rectangle fromItemIndex(int i)
        {
            return new Rectangle(pos.X + (i * scale * (selection.Width - 5)), pos.Y, selection.Width * scale, selection.Height * scale);
                
        }

        public static void Render()
        {
            Update();
            Basic.spriteBatch.Draw(tex, pos, Color.White);
            Basic.spriteBatch.Draw(selection, fromItemIndex(selectedItemIndex), Color.White);

            for(int i = 0; i < 10; i++)
            {
                if (items[i].render)
                    Basic.spriteBatch.Draw(items[i].tex, fromItemIndex(i), Color.White);
            }

            if (currentItem is ZeroItem == false)
                Basic.spriteBatch.DrawString(Basic.font, currentItem.name, new Vector2(pos.X, pos.Y - 60), Color.White);
        }
    }
}
